package org.example;
//
//import com.typesafe.config.ConfigFactory;
//import groovy.lang.Binding;
//import groovy.lang.GroovyShell;
//import org.apache.pekko.actor.*;
//import org.codehaus.groovy.ant.Groovy;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import scala.collection.Iterable;
//import scala.collection.Iterator;
//
//import java.lang.reflect.InvocationTargetException;
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;
//
//public class Worker extends AbstractActor implements ExecuteExpression{
public class Worker {
//    private static final Logger log = LoggerFactory.getLogger(Worker.class);
//
//    public static Props props(Object myObject) {
//        return Props.create(Worker.class, () -> new Worker(myObject));
//    }
//    private Object object;
//    public Worker(Object o){
//       this.object = 0 ;
//    }
//
//    public static void main(String[] args) {
//        ActorSystem actorSystem = ActorSystem.create("actorSystem", ConfigFactory.load());
//        ActorRef worker = actorSystem.actorOf(Worker.props("ssss"), "worker");
//        Message msg = new Message("" , MessageType.REGISTER , null);
//        worker.tell(msg , ActorRef.noSender());
//    }
//    @Override
//    public Receive createReceive() {
//        return receiveBuilder()
//                .match(Message.class , this::messageHandler)
//                .matchAny(this::handler)
//                .build();
//    }
//
//    private void messageHandler(Message message) {
//        switch (message.getType()) {
//            case DATA:
//                String methodName = (String) message.getMessage();
//                Method method = null ;
//                try {
//                    System.out.println(methodName);
//                    method = this.getClass().getMethod(methodName, message.getParemeterClass());
//                } catch (NoSuchMethodException e) {
//                    throw new RuntimeException(e);
//                }
//                try {
//                    System.out.println(method.getName());
//                    System.out.println(Arrays.toString(message.getParameters()));
//                    scala.collection.immutable.Map<String,Object> parameter = (scala.collection.immutable.Map)message.getParameters()[1];
//                    Map<String,Object> para = new HashMap<>();
//                    Iterable<String> keys = parameter.keys();
//                    Iterator<String> iterator = keys.iterator();
//                    while (iterator.hasNext()) {
//                        String next = (String)iterator.next();
//                        para.put(next , parameter.get(next));
//                    }
//                    Object invoke = method.invoke(this, new Object[]{
//                            (String)message.getParameters()[0]
//                            ,para
//                    });
//                    System.out.println(invoke);
//                } catch (IllegalAccessException e) {
//                    throw new RuntimeException(e);
//                } catch (InvocationTargetException e) {
//                    throw new RuntimeException(e);
//                }
//                break ;
//            case REGISTER:
//                System.out.println("register");
//                ActorSelection actorSelection
//                        = context().actorSelection("pekko://actorSystem@127.0.0.1:17355/user/master");
//                Message msg = new Message("pekko://actorSystem@127.0.0.1:17354/user/worker" , MessageType.REGISTER ,null);
//                actorSelection.tell(msg , getSelf());
//                break ;
//            default:
//                break ;
//        }
//    }
//
//    private Map<String,Integer> cnt = new HashMap<>();
//    public void handler(Object o){
//        if(o.toString().startsWith("register")){
//            ActorSelection actorSelection
//                    = context().actorSelection("pekko://actorSystem@127.0.0.1:17355/user/master");
//            actorSelection.tell("register::" + "pekko://actorSystem@127.0.0.1:17354/user/worker" , getSelf());
//        }else if("print".equals(o.toString())){
//            cnt.forEach((key,value)->{
//                System.out.println("key: " + key + " , value: " + value);
//            });
//        }else{
//            System.out.println("word: " + o.toString());
//            cnt.computeIfPresent(o.toString() , (String key , Integer cnt) -> cnt + 1);
//            cnt.putIfAbsent(o.toString(),1);
//        }
//    }
//
//    @Override
//    public Object execute(String expression , Map<String,Object> paras) {
//        Binding binding = new Binding();
//        paras.forEach(binding::setVariable);
//        GroovyShell shell = new GroovyShell(binding);
//        Object evaluate = shell.evaluate(expression);
//        return evaluate;
//    }
}
